/*
 * Copyright© 2003-2019 浙江汇信科技有限公司, All Rights Reserved.
 */
package com.icinfo.cloud.provider.punish.config.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.icinfo.cloud.common.security.util.UserUtil;
import com.icinfo.cloud.provider.common.constant.Constant;
import com.icinfo.cloud.provider.common.dicenum.IsUseEnum;
import com.icinfo.cloud.provider.common.exception.BusinessException;
import com.icinfo.cloud.provider.punish.api.dto.CaseTransferPageRequest;
import com.icinfo.cloud.provider.punish.config.dto.*;
import com.icinfo.cloud.provider.punish.config.mapper.ConfigRegisterCaseMapper;
import com.icinfo.cloud.provider.punish.config.model.ConfigRegisterCase;
import com.icinfo.cloud.provider.punish.config.model.ConfigRegisterCaseFile;
import com.icinfo.cloud.provider.punish.config.service.IConfigRegisterCaseFileService;
import com.icinfo.cloud.provider.punish.config.service.IConfigRegisterCaseService;
import com.icinfo.cloud.xzcf.provider.member.userinfo.dto.UserDTO;
import com.icinfo.framework.core.service.support.MyBatisServiceSupport;
import com.icinfo.framework.mybatis.pagehelper.PageHelper;
import com.icinfo.framework.tools.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 描述:  config_register_case 对应的Service接口实现类.<br>
 *
 * @author framework generator
 * @date 2022年11月30日
 */
@Service
public class ConfigRegisterCaseServiceImpl extends MyBatisServiceSupport implements IConfigRegisterCaseService {
    /**
     * 日志记录器
     */
    private static final Logger logger = LoggerFactory.getLogger(ConfigRegisterCaseServiceImpl.class);

    @Resource
    private ConfigRegisterCaseMapper configRegisterCaseMapper;

    @Resource
    private IConfigRegisterCaseFileService configRegisterCaseFileService;

    /**
     * 新增配置项
     * @param request
     * @return
     */
    @Override
    public int addConfig(ConfigRegisterCaseAddRequest request) {
        UserDTO userInfo = UserUtil.getUserInfo();
        if (ObjectUtil.isEmpty(userInfo)){
            return 0;
        }
        ConfigRegisterCase configRegisterCase = new ConfigRegisterCase();
        BeanUtil.copyProperties(request,configRegisterCase);
        if (StringUtils.isBlank(request.getId())){
            configRegisterCase.setId(StringUtils.uuid());
            // 默认0 未启用
            configRegisterCase.setState(Constant.STRING_0);
            configRegisterCase.setCreateTime(new Date());
            configRegisterCase.setUpdateTime(new Date());
            configRegisterCase.setCreateUserId(userInfo.getUserId().toString());
            configRegisterCase.setCreateUserName(userInfo.getRealName());
            configRegisterCase.setCreateOrgCode(userInfo.getOrgCode());
            configRegisterCase.setCreateOrgName(userInfo.getOrgName());
            configRegisterCase.setCreateDeptCode(userInfo.getDeptCode());
            return configRegisterCaseMapper.insert(configRegisterCase);
        } else {
            configRegisterCase.setUpdateTime(new Date());
            return configRegisterCaseMapper.updateByPrimaryKeySelective(configRegisterCase);
        }
    }

    /**
     * 配置项状态修改 是否启用；1启用 0未启用
     * @param request
     * @return
     */
    @Override
    public int editState(ConfigRegisterCaseEditStateRequest request) {
        UserDTO userInfo = UserUtil.getUserInfo();
        if (ObjectUtil.isEmpty(userInfo)){
            return 0;
        }
        ConfigRegisterCase configRegisterCase = new ConfigRegisterCase();
        configRegisterCase.setId(request.getId());
        configRegisterCase.setState(request.getState());
        configRegisterCase.setUpdateTime(new Date());
        return configRegisterCaseMapper.updateByPrimaryKeySelective(configRegisterCase);
    }

    /**
     * 配置列表分页查询
     * @param request
     * @return
     */
    @Override
    public List<ConfigRegisterCasePageResponse> pageList(ConfigRegisterCasePageRequest request) {
        if (request.getLength() > 200){
            throw new BusinessException("每页最多不超过200条数据");
        }
        //机构权限处理
        checkOrgCode(request);
        PageHelper.startPage(request.getPageNum(),request.getLength());
        List<ConfigRegisterCasePageResponse> list = configRegisterCaseMapper.pageList(request);
        list.forEach(e -> {
            e.setStateName(IsUseEnum.getExplainByCode(e.getState()));
        });
        return list;
    }

    /**
     * 增加材料配置
     * @param request
     * @return
     */
    @Override
    public int addMaterial(ConfigRegisterCaseAddMaterialRequest request) {
        ConfigRegisterCase configRegisterCase = configRegisterCaseMapper.selectByPrimaryKey(request.getRelId());
        if (ObjectUtil.isEmpty(configRegisterCase)){
            return 0;
        }
        for (ConfigRegisterCaseFile file : request.getFileList()) {
            file.setRelId(request.getRelId());
            file.setIsDelete(Constant.STRING_0);
            file.setCreateTime(new Date());
        }
        configRegisterCaseFileService.saveList(request.getFileList());
        return 1;
    }

    /**
     * orgCode处理
     *
     * @param queryDto
     * @return void
     * @author lxj
     * @date 2022/3/31
     **/
    private void checkOrgCode(ConfigRegisterCasePageRequest queryDto) {
        queryDto.setOrg(0);
        if (StringUtils.isEmpty(queryDto.getOrgCode())) {
            return;
        }
        List<String> orgCodeList = new ArrayList<>();
        List<String> deptCodeList = new ArrayList<>();
        String[] str = queryDto.getOrgCode().split(",");

        for (String s : str) {
            if(s.indexOf("_")>0){
                orgCodeList.add(s);
            }else {
                deptCodeList.add(s);
            }
        }
        if(orgCodeList.size()>0&&deptCodeList.size()>0){
            queryDto.setOrg(1);
        }else if(orgCodeList.size()>0){
            queryDto.setOrg(2);
        }else if(deptCodeList.size()>0){
            queryDto.setOrg(3);
        }

        queryDto.setOrgCodeList(orgCodeList);
        queryDto.setDeptCodeList(deptCodeList);
    }
}
